<template>
  <div>
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>

</template>
<script>
import { getAllRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    closeDialog() {
      this.$emit('close')
    },
    async loadRoles() {
      try {
        const { data } = await getAllRoles(1, 100)
        console.log(data)
        this.list = data.rows
        const rs = await getUserDetailById(this.employeeId)
        console.log(rs)
        this.checkList = rs.data.roleIds
      } catch (error) {
        console.dir(error)
      }
    },
    hSubmit() {
      this.doassignRoles()
    },
    async doassignRoles() {
      try {
        await assignRoles({ id: this.employeeId, roleIds: this.checkList })
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
